package com.aspire.core.match.ua;

import java.util.List;
import jodd.cache.Cache;
import jodd.cache.TimedCache;

import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;

import com.aspire.core.match.ua.object.Device;
import com.aspire.core.match.ua.object.DeviceBrand;
import com.aspirecn.core.ShareCommon;
import com.aspirecn.core.commons.Configuration;
import com.aspirecn.core.commons.Constant;
import com.aspirecn.core.commons.ObjectUtils;
import com.aspirecn.core.commons.UserAgentUtils;
import com.aspirecn.core.datacache.UACache;
import com.aspirecn.core.datacache.useragent.DeviceBrandCache;
import com.aspirecn.core.datacache.useragent.DeviceCache;
import com.aspirecn.core.datacache.useragent.UserAgentCommonCache;

/**
 * 正常请使用 ShareCommon.getInstance().getUACache
 * 
 * @author wangxinning
 */
public class UACenter {

	//public static Logger log = Logger.getLogger("uafile");
	//public static Logger faillog = Logger.getLogger("uacenter");
	public final static Logger logger_ua = Logger.getLogger("uamatchlog");

	private UACache uaCache = ShareCommon.getInstance().getUACache();
	private DeviceCache deviceCache = ShareCommon.getInstance().getDeviceCache();
	private DeviceBrandCache deviceBrandCache = ShareCommon.getInstance().getDeviceBrandCache();

	public void reload() {
		uaCache.reload();
	}

	//加入新逻辑
	public Device getDevice(int device_id) {

		Device retDevice = null;
		
//		//非MM系统中采用新适配规则
//		if( !Configuration.getInstance().isMM())
//		{
			retDevice = deviceCache.getDevice(device_id);
//		}
			
		if( retDevice == null )
		{
			retDevice = uaCache.getDevice(device_id);
			
			//mm取不到再去这取一下
			if( retDevice == null )
			{
				retDevice = deviceCache.getDevice(device_id);
			}
		}
		
		return retDevice;
	}

	//加入新逻辑
	public DeviceBrand getBrand(int brandID) {
		
		DeviceBrand retDeviceBrand = null;
		
//		if( !Configuration.getInstance().isMM())
//		{
			retDeviceBrand = deviceBrandCache.getDeviceBrand(brandID);
//		}
		
		if( retDeviceBrand == null )
		{
			retDeviceBrand = uaCache.getDeviceBrand(brandID);
			
			if( retDeviceBrand == null )
			{
				retDeviceBrand = deviceBrandCache.getDeviceBrand(brandID);
			}
		}
		
		return retDeviceBrand;
	}

	// android特殊适配数据
	public final static Integer[] android_deviceids = Constant.android_deviceids;

	public List<DeviceBrand> searchBrand(String keyword) {

		List<DeviceBrand> retList = null;		
		if( !Configuration.getInstance().isMM())
		{
			retList = deviceBrandCache.searchBrand(keyword);
		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.searchBrand(keyword);
		}
		
		return retList;

	}

	public List<Device> searchDevice(String keyword) {

		List<Device> retList = null;		
		if( !Configuration.getInstance().isMM())
		{
			retList = deviceCache.searchDevice(keyword);
		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.searchDevice(keyword);
		}
		
		return retList;

	}

	public List<Device> searchDevice(int brandid, String keyword) {

		List<Device> retList = null;		
		if( !Configuration.getInstance().isMM())
		{
			retList = deviceCache.searchDevice(brandid, keyword);			
		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.searchDevice(brandid, keyword);
		}
		
		return retList;
	}

	public List<Device> getBrandDeviceList(int brandID) {

		List<Device> retList = null;		
		if( !Configuration.getInstance().isMM())
		{
			retList = deviceCache.getBrandDeviceList(brandID);
		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.getBrandDeviceList(brandID);
		}
		
		return retList;
	}

	
	//json版数据适配
	public Device matchUA_old( String ua )
	{
		Device oldDevice = uaCache.matchUA(ua);
		return oldDevice;
	}	
	
	//csv版数据适配
	public Device matchUA_new( String ua )
	{
		Device newDevice = null;
		try {
			newDevice = UserAgentUtils.matchUA(ua);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return newDevice;
	}

//	
//	//缓存
//	private final static int CACHE_SIZE = 2000;
//	//private Map<String,Device> uaCacheMap = new ConcurrentHashMap<String,Device>(CACHE_LIMIT);
//	private Cache<String,Device> cacheService = new LRUCache<String,Device>(CACHE_SIZE);
	
	//加入新逻辑
	public Device matchUA(String ua) {
		
//		if( uaCacheMap.containsKey(ua) )
//		{
//			Device cacheDevice = uaCacheMap.get(ua);
//			return cacheDevice;
//		}		
//		
//		if( uaCacheMap.size() >= CACHE_LIMIT )
//		{
//			uaCacheMap.clear();
//		}
		
		Device cacheDevice = UserAgentCommonCache.getFromCache( ua );
		if( cacheDevice != null )
		{
			if( cacheDevice.getDevice_id() == UserAgentCommonCache.NULL_DEVICE_ID )
			{
				return null;
			}
			else
			{
				return cacheDevice;
			}
		}
				
//		if( cacheService.containsKey(ua) )
//		{
//			Device cacheDevice = uaCacheMap.get(ua);
//			return cacheDevice;
//		}	
		

		
		Device mmDevice = null;
		Device newDevice = null;
		
		if( Configuration.getInstance().isMM())
		{
			mmDevice = matchUA_old(ua);			
		}
		
		try {
			newDevice = matchUA_new(ua);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		//非MM，直接返回新方式
		if( !Configuration.getInstance().isMM() )
		{
			//uaCacheMap.put(ua, newDevice);
			UserAgentCommonCache.addToCache(ua, newDevice);
			return newDevice;
		}
		
		//判断返回值部分
		//1.mmDevice为空
		if( mmDevice == null && newDevice != null )
		{
			//uaCacheMap.put(ua, newDevice);
			UserAgentCommonCache.addToCache(ua, newDevice);
			return newDevice;
		}
		
		//2.都不为空
		if( mmDevice != null && newDevice != null )
		{
			//新增机型
			if( uaCache.getDevice(newDevice.getDevice_id()) == null )
			{
				//uaCacheMap.put(ua, newDevice);
				UserAgentCommonCache.addToCache(ua, newDevice);
				return newDevice;
			}
		}
		
		//uaCacheMap.put(ua, mmDevice);
		UserAgentCommonCache.addToCache(ua, mmDevice);
		return mmDevice;
	}
	
//	private final static int NULL_DEVICE_ID = -999;
//	private void addToCache( String ua, Device device )
//	{
//		if( device == null )
//		{
//			Device tempDevice = new Device();
//			tempDevice.setDevice_id(NULL_DEVICE_ID);
//			cacheService.put(ua, tempDevice);
//		}
//		else
//		{
//			cacheService.put(ua, device);
//		}
//	}
//	
//	private Device getFromCache( String ua )
//	{
//		Device device = cacheService.get(ua);
//		return device;	
//	}	

	// 不可匹配的保存在成员对象中，下次判断如果存在则不进行匹配操作
	public List<DeviceBrand> getDeviceBrandList() {
		
		List<DeviceBrand> retList = null;
		
		if( !Configuration.getInstance().isMM())
		{
			try {
				retList = deviceBrandCache.getDeviceBrandList();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.getDeviceBrandList();
		}

		return retList;
	}
	

	public List<Device> getDeviceListByBrandID(int brandID) {
		return this.getBrandDeviceList(brandID);
	}

	//private static Cache brandAlphaListCache = new TimedCache( DateUtils.MILLIS_PER_HOUR );
	
	public List<String> getBrandAlphaList(int brandID) {

		List<String> retList = null;
		
//		if( !Configuration.getInstance().isMM())
//		{
			try {
				retList = deviceCache.getBrandAlphaList(brandID);
			} catch (Exception e) {
				e.printStackTrace();
			}
//		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.getBrandAlphaList(brandID);
		}
		
		return retList;

	}

	public List<Device> getBrandAlphaDeviceList(int brandID, String firstAlpha) {

		List<Device> retList = null;
		
//		if( !Configuration.getInstance().isMM())
//		{
			try {
				retList = deviceCache.getBrandAlphaDeviceList(brandID, firstAlpha);
			} catch (Exception e) {
				e.printStackTrace();
			}
//		}
		
		if( ObjectUtils.isEmptyList(retList) )
		{
			retList = uaCache.getBrandAlphaDeviceList(brandID, firstAlpha);
		}
		
		return retList;
	}

	public String formatUA(String ua) {

		return uaCache.formatUA(ua);

	}

	public void load() {

	}
}